package org.chnxi.study.springcloud.dubbo.shiro.auth.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.chnxi.study.springcloud.dubbo.shiro.auth.entity.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class LoginController {

    @GetMapping("/login")
    public String login(User user){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getName(), user.getPassword());

        try {
            subject.login(token);
        }catch (AuthenticationException e){
            return "错误的用户名或密码";
        }catch (AuthorizationException e){
            return "没有权限";
        }
        return "登录成功";

    }

    @GetMapping("/index")
    public String index(){
        return "idx Page";
    }

    public void recTicket(String ticket, HttpServletRequest request, HttpServletResponse response){

    }

}
